package com.pz.cloud.framework.student.job;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.pz.cloud.framework.base.model.Pageable;
import com.pz.cloud.framework.business.pzStudent.entity.PzTeacher;
import com.pz.cloud.framework.business.pzStudent.service.IPzTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
public class PullTeacherJob implements Job {

    @Autowired
    private IPzTeacherService pzTeacherService;

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(" --- 拉取教职工信息调度开始 --- ");
        PzTeacher entity = new PzTeacher();
        int size = 1000;
        int count = pzTeacherService.count();
        int step = 1;
        while (count > 0) {
            int current = count > size ? size : count;
            Pageable pageable = new Pageable();
            pageable.setPageNo(step++);
            pageable.setPageSize(current);
            List<PzTeacher> list = pzTeacherService.findPage(entity, pageable);
            list.forEach(r -> {
                SysUser sysUser = sysUserService.getUserByName(r.getGH());
                if (sysUser == null) {
                    sysUser = new SysUser();
                    sysUser.setUsername(r.getGH());
                    sysUser.setRealname(r.getXM());
                    sysUser.setDelFlag(0);
                    sysUser.setWorkNo(r.getGH());
                    if (r.getXBM() != null && r.getXBM().trim().length() > 0) {
                        sysUser.setSex(Integer.parseInt(r.getXBM()));
                    }
                    sysUser.setPhone(r.getFILED3());
                    boolean save = sysUserService.save(sysUser);
                    if (save) {
                        log.info("教师用户入库完成：{}", r.getGH());
                    }
                    else {
                        log.info("教师用户入库失败：{}", r.getGH());
                    }
                }
                else if (!Objects.equals(sysUser.getRealname(), r.getXM()) ||
                        !Objects.equals(sysUser.getPhone(), r.getFILED3()) ||
                        StringUtils.isBlank(sysUser.getWorkNo())) {
                    sysUser.setRealname(r.getXM());
                    sysUser.setPhone(r.getFILED3());
                    sysUser.setWorkNo(r.getGH());
                    if (sysUserService.updateById(sysUser)) {
                        log.info("用户更新成功：{}", r.getGH());
                    }
                    else {
                        log.info("用户更新失败：{}", r.getGH());
                    }
                }
            });
            count -= size;
        }
        log.info(" --- 拉取教职工信息调度结束 --- ");
    }

}
